package com.cloud2022.kafka.util;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Collection;

/**
 * @author: create by God of SJF
 * @version: v1.0
 * @description: com.cloud2022.kafka.util
 * @date:2021/12/20
 */
public class StaticMethod {
    public synchronized  static void writeFile(String filePath, Collection<String> lineList)throws Exception {
        File copy = new File(filePath.substring(0,filePath.lastIndexOf("/")));
//        File copy = new File(filePath);
        if (!copy.exists()) {
            copy.mkdirs();
        }
        try {
            FileOutputStream fout = new FileOutputStream(filePath, true);
            for (String string : lineList) {
                fout.write(string.getBytes("utf-8"));
            }
            fout.flush();
            fout.close();
        } catch (Exception e) {
            throw e;
        }
    }

    public static void writeString(String filePath, String str){
        try {
            File copy = new File(filePath.substring(0, filePath.lastIndexOf("\\")));
            if (!copy.exists()) {
                copy.mkdirs();
            }

            FileOutputStream fout = new FileOutputStream(filePath, true);
            fout.write(str.getBytes("utf-8"));
            fout.flush();
            fout.close();
        } catch (Exception e) {
            System.err.println("writeString 文件写入异常"+e);
        }
    }

}
